import 'package:flutter/material.dart';
import 'package:macnov/utils/colorUtil.dart';

class ProgressWidget extends StatelessWidget {
  final String spent;
  final String assigned;
  final String unassigned;

  final double height = 10;
  final double radius = 5;

  ProgressWidget(this.spent, this.assigned, this.unassigned);

  Color unassignedColor() {
    return ColorsUtil.hexColor(0x55F6FE);
  }

  Color assignedColor() {
    return ColorsUtil.hexColor(0x0AADEC);
  }

  Color spentColor() {
    return ColorsUtil.hexColor(0x376BD1);
  }

  double calcAssignedRatio() {
    double a = 0.0;
    try {
      a = double.parse(assigned);
    } catch (e) {
      a = 0.0;
    }
    double u = 0.0;

    try {
      u = double.parse(unassigned);
    } catch (e) {
      u = 0.0;
    }
    return a / (a + u);
  }

  double calcUnassignedRatio() {
    double a = 0.0;
    try {
      a = double.parse(assigned);
    } catch (e) {
      a = 0.0;
    }
    double u = 0.0;
    try {
      u = double.parse(unassigned);
    } catch (e) {
      u = 0.0;
    }
    return u / (a + u);
  }

  double calcSpentRatio() {
    double a = 0.0;
    try {
      a = double.parse(assigned);
    } catch (e) {
      return 0.0;
    }
    double s = 0.0;
    try {
      s = double.parse(spent);
    } catch (e) {
      s = 0.0;
    }
    return s / a;
  }

  Widget _buildAssign(context) {
    final width = MediaQuery.of(context).size.width;
    double assignedWidth = calcAssignedRatio() * width;
    if (assignedWidth.isNaN) return Container();
    print('_buildAssign $assignedWidth');

    return Positioned(
      left: 0,
      child: Container(
        width: assignedWidth,
        height: height,
        decoration: BoxDecoration(color: assignedColor()),
      ),
    );
  }

  Widget _buildSpent(context) {
    final width = MediaQuery.of(context).size.width;
    double spentWidth = calcSpentRatio() * calcAssignedRatio() * width;
    print('_buildSpent $spentWidth');
    if (spentWidth.isNaN) return Container();
    return Positioned(
      left: 0,
      child: ClipRRect(
        child: Container(
          width: spentWidth,
          height: height,
          decoration: BoxDecoration(color: spentColor()),
        ),
      ),
    );
  }

  Widget _buildTotal(context) {
    final width = MediaQuery.of(context).size.width;
    return Positioned(
      left: calcAssignedRatio() * width,
      child: ClipRRect(
        borderRadius: BorderRadius.only(
            bottomRight: Radius.circular(radius),
            topRight: Radius.circular(radius)),
        child: Container(
          width: calcUnassignedRatio() * width,
          height: height,
          decoration: BoxDecoration(color: unassignedColor()),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    return SizedBox(
      width: width,
      height: height,
      child: ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(radius)),
          child: Stack(
            children: <Widget>[
              _buildAssign(context),
              _buildSpent(context),
              _buildTotal(context),
            ],
          )),
    );
  }
}
